<?php
/**
 * Created by PhpStorm.
 * User: Charles
 * Date: 2017/10/11
 * Time: 12:07
 */

namespace App\Models;

use DB;
use Illuminate\Support\Facades\Cache;

class Settings
{
    private static $settings = [];

    public static function get($key)
    {
        if (array_key_exists($key, self::$settings)) {
            return self::$settings[$key];
        }
        $line = DB::table('settings')->where('key', $key)->first();
        if (null === $line) {
            return (self::$settings[$key] = env($key, null));
        }
        return (self::$settings[$key] = $line->value);
    }

    public static function set($key, $value)
    {
        $line = DB::table('settings')->where('key', $key)->first();
        if (null === $line) {
            DB::table('settings')->insert(compact('key', 'value'));
        } else {
            DB::table('settings')->where(compact('key'))->update(compact('value'));
        }
        SettingsCache::forget($key);
        self::$settings[$key] = $value;
    }
}